package com.lym.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lym.reggie.common.CustomException;
import com.lym.reggie.common.R;
import com.lym.reggie.entity.Category;
import com.lym.reggie.entity.Dish;
import com.lym.reggie.entity.DishDto;
import com.lym.reggie.entity.DishFlavor;
import com.lym.reggie.mapper.DishFlavorMapper;
import com.lym.reggie.mapper.DishMapper;
import com.lym.reggie.service.CategoryService;
import com.lym.reggie.service.DishFlavorService;
import com.lym.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    @Override
    @Transactional  //加事务控制，因为涉及两张表，所以要加上来保证事务的一致性。注意Application类上需加@EnableTransactionManagement注解！
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        Long dishId = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();
        //保存进DishFlavor
        //利用stream流
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        /*for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }*/

        dishFlavorService.saveBatch(flavors);
    }

    //数据回显
    @Override
    public DishDto getByIdWithFlavor(long id) {
        // 1. 根据Dish的id查询菜品的基本信息  从dish表查询
        Dish dish = this.getById(id);

        // 2. 根据dish的id查询DishFavpor表的...得到列表
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper);

        //然后把查询出来的flavors数据set进行 DishDto对象
        DishDto dishDto = new DishDto();
        //把dish表中的基本信息copy到dishDto对象，因为才创建的dishDto里面的属性全是空
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavorList);

        return dishDto;
    }

    //修改菜品
    @Override
    @Transactional  //加事务控制，因为涉及两张表，所以要加上来保证事务的一致性。注意Application类上需加@EnableTransactionManagement注解！
    public void updateWithFlavor(DishDto dishDto) {
        //涉及dish dishFlavor两个表
        //1.修改dish表
        this.updateById(dishDto);
        //2.修改DishFlavor表
        // 2.1 清理  获取dishId  根据此移除
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        // 2.2 加入
        List<DishFlavor> flavorList = dishDto.getFlavors();
        //遍历
        //下面这段流的代码我注释,然后测试，发现一次是报dishId没有默认值(先测)，两次可以得到结果(后测，重新编译过，清除缓存过),相隔半个小时
        //因为这里拿到的flavorsz只有name和value(这是在设计数据封装的问题),不过debug测试的时候发现有时候可以拿到全部数据,有时候又不可以...  所以还是加上吧。。。。。
        flavorList = flavorList.stream().map(flavor->{
            flavor.setDishId(dishDto.getId());
            return flavor;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavorList);

    }

    //逻辑删除
    @Override
    @Transactional
    public void deleteWithFlavor(List<Long> ids) {
        //Dish DishFlavor两张表   查询是否在售卖，若在售卖抛出异常 conroller再去处理异常?
        //如果status为0则可以删除，否则抛出异常
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.in(Dish::getId,ids);
        List<Dish> dishList = this.list(dishQueryWrapper);

        for (Dish dish : dishList) {
            if (dish.getStatus()==0){
                //1. 根据id删除DishFla对应的记录
                LambdaQueryWrapper<DishFlavor> flavorQueryWrapper = new LambdaQueryWrapper<>();
                flavorQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
                dishFlavorService.remove(flavorQueryWrapper);
                //2. 根据ID删除Dish
                this.removeById(dish.getId());
            }else {
                throw new CustomException("删除菜品中有正在售卖菜品，无法全部删除！");
            }
        }


    }
}
